#include "widget.h"
#include "ui_widget.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QMessageBox>
#include <carmainwindow.h>
#include <QDebug>


Widget::Widget(QWidget *parent): FramelessWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->initForm();
    //ui->pushButton->setIcon(":/new/prefix1/icon/1.png")

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //2、打开数据库文件，如果不存在，则自动创建 ，指定数据库文件的名字
    db.setDatabaseName("accont.db");
    bool ret = db.open();
    if(ret == false)
    {
        qDebug()<< db.lastError().text();
        return;
    }
    QString sql = QString("create table if not exists info(accont varchar(20),password varchar(20));");
    //执行上面组合好的sql语句
     //实例化一个数据库操作类的对象
     QSqlQuery query;
     if(!query.exec(sql))
     {
         qDebug()<<"create table error "<<query.lastError().text();
     }




}


Widget::~Widget()
{
    delete ui;
}

//注册插入数据
void Widget::on_btn_regite_clicked()
{

 QString accont = ui->lineEdit_accont->text();
  QString passwd = ui->lineEdit_password->text();
 if(accont.isEmpty() || passwd.isEmpty())
 {
     QMessageBox::information(this,"注册","账号为空！！！");
     return;
 }

    //先准备插入数据的sql语句
    QString sql = QString("insert into info values(\"%1\",\"%2\");")
            .arg(ui->lineEdit_accont->text())
            .arg(ui->lineEdit_password->text());


    qDebug()<<sql;

    //执行sql语句
    QSqlQuery query;
    if(!query.exec(sql))
    {
        qDebug()<<"insert into error "<<query.lastError().text();
    }

    ui->lineEdit_password->clear();
}
//登录遍历数据库有没有对应的账号密码
void Widget::on_btn_login_clicked()
{

  QString  accont_login = ui->lineEdit_accont->text();
  QString password_login = ui->lineEdit_password->text();


  if(accont_login.isEmpty() || password_login.isEmpty())
  {
      QMessageBox::information(this,"登录","密码为空！！！");
      return;
  }

    QString sql = QString("select * from info;");
    QSqlQuery query;

    if(query.exec(sql) == false)
    {
        qDebug()<<"select error "<<query.lastError().text();
    }
    while(1)
    {
        if(query.next() == false)
            break;

        //获取这一条记录中的 name字段的数据
         accont = query.value("accont").toString();
        //获取age字段
         password = query.value("password").toString();
    }
    if(password != password_login)
    {
     QMessageBox::information(this,"注册","密码错误！！！");
    }
     else if(accont == accont_login&&password == password_login)
    {
        CarMainWindow *widget = new CarMainWindow;
        widget->show();
        this->close();
    }

}

void Widget::initForm()
{
    //设置标题栏控件
    ui->labTitle->setText("登录/注册");
    this->setWindowTitle(ui->labTitle->text());
    this->setTitleBar(ui->labTitle);

    //关联信号
    connect(this, SIGNAL(titleDblClick()), this, SLOT(titleDblClick()));
    connect(this, SIGNAL(windowStateChange(bool)), this, SLOT(windowStateChange(bool)));

    //设置样式表
    QStringList list;
    list << "#titleBar{background:#6CE2BB;}";
    list << "#titleBar{border-top-left-radius:8px;border-top-right-radius:8px;}";
    list << "#widgetMain{border:2px solid #A2E9D5;background:#A2E9D5;}";
    //list << "#widgetMain{border-bottom-left-radius:8px;border-bottom-right-radius:8px;}";
    this->setStyleSheet(list.join(""));
}

void Widget::titleDblClick()
{
    on_btnMenu_Max_clicked();
}

void Widget::windowStateChange(bool max)
{
    ui->btnMenu_Max->setText(max ? "还原" : "最大");
}

void Widget::on_btnMenu_Min_clicked()
{
#ifdef Q_OS_MACOS
    this->setWindowFlags(this->windowFlags() & ~Qt::FramelessWindowHint);
#endif
    this->showMinimized();
}

void Widget::on_btnMenu_Max_clicked()
{
    if (this->isMaximized()) {
        this->showNormal();
        ui->btnMenu_Max->setText("最大");

    } else {
        this->showMaximized();
        ui->btnMenu_Max->setText("还原");
    }
}

void Widget::on_btnMenu_Close_clicked()
{
    this->close();

}
